//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.yaukie.base.system;

import java.util.TimerTask;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import javax.annotation.PreDestroy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.yaukie.base.util.SpringContextUtil;
import org.yaukie.base.util.StringTools;

@Component
public class ASyncManager {
    private static final Logger log = LoggerFactory.getLogger(ASyncManager.class);
    public ASyncManager aSyncManager = null;
    private ScheduledExecutorService executorService = (ScheduledExecutorService)SpringContextUtil.getBean("executorService");

    public ASyncManager() {
    }

    @PreDestroy
    public void shutdown() {
        log.info("关闭线程池执行器{}", this.executorService);
        ASyncManager.ThreadsManager.forceShutdownAndTerminate(this.executorService);
    }

    public void execute(TimerTask task) {
        this.executorService.schedule(task, 10L, TimeUnit.MILLISECONDS);
    }

    public static class ThreadsManager {
        public ThreadsManager() {
        }

        public static void sleep(long time) {
            try {
                Thread.sleep(time);
            } catch (InterruptedException var3) {
                Thread.currentThread().interrupt();
                ASyncManager.log.error("线程池出现异常,立即中断当前线程{}", var3);
            }

        }

        public static void printException(Runnable r, Throwable t) {
            if (t == null && r instanceof Future) {
                Future future = (Future)r;

                try {
                    future.get();
                } catch (InterruptedException var4) {
                    Thread.currentThread().interrupt();
                } catch (ExecutionException var5) {
                    t = var5.getCause();
                } catch (CancellationException var6) {
                    t = var6.getCause();
                }
            }

            if (ASyncManager.log.isErrorEnabled()) {
                ASyncManager.log.error(t.getMessage(), t);
            }

        }

        public static void forceShutdownAndTerminate(ExecutorService executorService) {
            if (!StringTools.isNull(executorService) && !executorService.isShutdown()) {
                executorService.shutdown();

                try {
                    if (!executorService.awaitTermination(200L, TimeUnit.SECONDS)) {
                        executorService.shutdownNow();
                        if (!executorService.awaitTermination(200L, TimeUnit.SECONDS) && ASyncManager.log.isErrorEnabled()) {
                            ASyncManager.log.error("executorService does not shutdown !");
                        }
                    }
                } catch (InterruptedException var2) {
                    executorService.shutdownNow();
                    Thread.currentThread().interrupt();
                    ASyncManager.log.error("线程池出现异常,立即中断当前线程{}", var2);
                }
            }

        }
    }
}
